
a = '''#230324 15:36:05 server id 3320043306  end_log_pos 9400 CRC32 0xe198bb93        Query   thread_id=46    exec_time=0     error_code=0 '''
b = ''''#230324 15:36:05 server id 3320043306  end_log_pos 9524 CRC32 0x1dfb017e        Xid = 275 '''
c = '''#230328  2:31:49 server id 2370520119  end_log_pos 502 CRC32 0x31e94796 	Query	thread_id=9574590	exec_time=0	error_code=0
'''
d = '''#230328  2:31:49 server id 2370520119  end_log_pos 584 CRC32 0xe81a66a9 	Table_map: `ghsmall`.`ghs_b_goods_search` mapped to number 359394
#'''
# start_time = None
# # import re
# # pattern_xid_start = re.compile(r'.+(Query   thread_id=).+')
# # if pattern_xid_start.search(a):
# #     start_time = a.split(' ')[1]
# #     print(start_time)
# if start_time is None :
#     print(d.split()[1])

#parse_binglogfile_name='mysql-bin.007744.txt'
# with open(parse_binglogfile_name, 'r', encoding='utf-8') as f:
#     file_content_list = f.readlines()
#     print(len(file_content_list))
#     f.close()
#


# start = [5,10,15,20]
# end = [1,6,11,16]
# if end[0] < start[0]:
#     print(1)
#     del end[0]
# if end[-1] < start[-1]:
#     del start[-1]
# print(start,end)

import subprocess
# 要执行的命令
# command = "grep -n  -o 'BEGIN' mysql-bin.007744_3.txt | awk -F ':' '{print $1}'"
#
# # 执行命令
# process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#
# # 等待命令执行完成
# process.wait()
#
# # 获取命令的输出和错误信息
# output = process.stdout.readlines()
# print(output)
# # #subprocess.Popen(command, stdout=subprocesst = subprocess.Popen('pwd', stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).stdout.readline()
# command = '''
# awk '/BEGIN/{printf "%d,",NR} END{print ""}' mysql-bin.007744_3.txt | sed 's/,$//; s/.*/[&]/'
# '''
# st = subprocess.Popen('command', stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True).stdout.readline()
# print(bytes.decode(st))
# # /usr1

# command = '''
# awk '/BEGIN/{printf "%d,",NR} END{print ""}' mysqlbinlog.txt | sed 's/,$//; s/.*/[&]/'
# '''
# #awk '/BEGIN/{printf '%d,',NR} END{print ""}' mysqlbinlog.txt | sed 's/,$//; s/.*/[&]/'
# -*- coding:utf-8 -*-
# import os
# command="grep -n 'BEGIN' mysql-bin.007744_3.txt | awk -F ':' '{print $1}'"
# print(list(os.system(command)))



# p=subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# print(p.stdout.readline())
# a = subprocess.getstatusoutput(command)
# print(a[1])
#
binlog_file = 'mysql-bin.000004'
cmd = '''
mysqlbinlog mysql-bin.007744 --base64-output=decode-rows -v | egrep '### INSERT INTO|### UPDATE|### DELETE FROM|Xid =|BEGIN|COMMIT|ROLLBACK|Table_map:'
'''
#
cmd = '''
mysqlbinlog mysql-bin.000004 --base64-output=decode-rows -v | egrep '### INSERT INTO|### UPDATE|### DELETE FROM|Xid =|BEGIN|COMMIT|ROLLBACK|Table_map:'
'''
# # 运行mysqlbinlog命令，获取输出并解析事务大小、执行时间和DML操作统计信息
# fdout = open("mysql_binlog_file_out.log",'w')
# #fderr = open("file_err.log",'w')
# process = subprocess.Popen(cmd, shell=True, stdout=fdout)
# if process.poll():
#     print(1)
#output = subprocess.check_output(cmd)
output = subprocess.getstatusoutput(cmd)


with open("mysql_binlog_file_out.txt_1",'w') as f:
    # subprocess.Popen(cmd, shell=True, stdout=f)
    f.write(output[1])
f.close()
with open("mysql_binlog_file_out.txt_1",'r') as f:
    print(f.readlines())
    f.close()

exit(0)
import ast
# parse_binglogfile_name='mysql_binlog_file_out.log'
# command_begin = '''
# awk '/^BEGIN/{printf "%d,",NR} END{print ""}'  parse_binglogfile_name | sed 's/,$//; s/.*/[&]/'
# '''
# command_begin = command_begin.replace('parse_binglogfile_name',parse_binglogfile_name)
# print(command_begin)
# p = subprocess.getstatusoutput(command_begin)
# print(p)
# trans_start_list = ast.literal_eval(p[1])
# print(trans_start_list)






